package com.joysuch.wwyt.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Splitter;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.risk.entity.RiskControllevels;
import com.joysuch.wwyt.risk.entity.RiskLevelDefaultControlLevel;
import com.joysuch.wwyt.risk.entity.dto.RiskLevelDefaultControlLevelDto;
import com.joysuch.wwyt.risk.mapper.RiskLevelDefaultControlLevelMapper;
import com.joysuch.wwyt.risk.service.RiskControlLevelService;
import com.joysuch.wwyt.risk.service.RiskLevelDefaultControlLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author heqifeng
 * @since 2021-09-02
 */
@Service
public class RiskLevelDefaultControlLevelServiceImpl extends ServiceImpl<RiskLevelDefaultControlLevelMapper, RiskLevelDefaultControlLevel> implements RiskLevelDefaultControlLevelService {

    @Autowired
    RiskLevelDefaultControlLevelMapper mapper;
    @Autowired
    RiskControlLevelService riskControllevelsService;

    @Override
    public boolean insertOrUpdate(RiskLevelDefaultControlLevelDto[] levelDtos) {
        ArrayList<RiskLevelDefaultControlLevel> riskControls = new ArrayList<>();
        for (RiskLevelDefaultControlLevelDto levelDto : levelDtos) {
            RiskLevelDefaultControlLevel riskControl = new RiskLevelDefaultControlLevel();
            List<String> levelCodeList = levelDto.getControlLevelCodes();
            if (!CollectionUtils.isEmpty(levelCodeList)) {
                String levelCodes = levelCodeList.stream().collect(Collectors.joining(Constant.SPLIT_COMMA));
                riskControl.setControlLevelCodes(levelCodes);
            }else {
                riskControl.setControlLevelCodes(Constant.EMPTY_STR);
            }
            riskControl.setId(levelDto.getId());
            riskControl.setRiskLevelCode(levelDto.getRiskLevelCode());
            riskControl.setOrgCode(Context.getCompanyCode());
            riskControl.setTenentId(Context.getCurrentTenantId());
            riskControls.add(riskControl);
        }
        return saveOrUpdateBatch(riskControls);
    }

    @Override
    public List<RiskLevelDefaultControlLevelDto> findAll() {
        LambdaQueryWrapper<RiskLevelDefaultControlLevel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RiskLevelDefaultControlLevel::getOrgCode,Context.getCompanyCode());
        wrapper.eq(RiskLevelDefaultControlLevel::getTenentId,Context.getCurrentTenantId());
        wrapper.eq(RiskLevelDefaultControlLevel::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
        List<RiskLevelDefaultControlLevel> riskControls = list(wrapper);
        ArrayList<RiskLevelDefaultControlLevelDto> riskLevelDefaultControlLevelDtos = new ArrayList<>();
        for (RiskLevelDefaultControlLevel riskControl : riskControls) {
            RiskLevelDefaultControlLevelDto riskLevelDefaultControlLevelDto = new RiskLevelDefaultControlLevelDto();
            riskLevelDefaultControlLevelDto.setRiskLevelCode(riskControl.getRiskLevelCode());
            riskLevelDefaultControlLevelDto.setId(riskControl.getId());
            List<String> controlCodes = Splitter.on(Constant.SPLIT_COMMA).trimResults().splitToList(riskControl.getControlLevelCodes());
            for (String controlCode : controlCodes) {
                if (StringUtils.isNotBlank(controlCode)) {
                    RiskControllevels controlLevels = riskControllevelsService.getByCode(Long.parseLong(controlCode));
                    if (controlLevels != null) {
                        riskLevelDefaultControlLevelDto.getControlLevels().add(controlLevels);
                    }
                }
            }
            riskLevelDefaultControlLevelDtos.add(riskLevelDefaultControlLevelDto);
        }
        return riskLevelDefaultControlLevelDtos;
    }

    @Override
    public List<RiskLevelDefaultControlLevel> selectByControlLevelCode(String code) {
        return mapper.selectByControlLevelCode(code,Context.getCompanyCode(),Context.getCurrentTenantId());
    }
}
